<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

    <!-- 可选的 Bootstrap 主题文件（一般不用引入） -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">

    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js" type="text/javascript" charset="utf-8"></script>
    <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</head>
<body>
<div class="container">
    <div class="row">
        <div class="col-md-2"></div>
        <div class="col-md-8">
            <p class="bg-primary" id="chatroom">
                1706A
            </p>
        </div>
        <div class="col-md-2"></div>
    </div>

    <div class="row">
        <div class="col-md-2"></div>
        <div class="col-md-8">

            <div class="input-group">
            <span class="input-group-btn">
              <button class="btn btn-default" type="button">消息</button>
            </span>
                <input type="text" id="mymessge" class="form-control" placeholder="请输入消息">
            </div><!-- /input-group -->

        </div>
        <div class="col-md-2"></div>
    </div>

    <div class="row">
        <div class="col-md-2"></div>
        <div class="col-md-8">
            <button type="button" class="btn btn-lg btn-primary" onclick="doConnect();">连接</button>
            <button type="button" class="btn btn-lg btn-success" onclick="doSendMessage();">发送</button>
            <button type="button" class="btn btn-lg btn-primary" onclick="disConnect();">关闭</button>
        </div>
        <div class="col-md-2"></div>
    </div>

</div>
<script type="text/javascript">
    var server = null;
    function doConnect() {
        if(server != null) {
            console.log("已连接！！！");
            return;
        }
        let ws = new WebSocket("ws://localhost:8080/websocket");
        ws.onopen = function() {
            console.log("client：打开连接");
            server = ws;
            ws.send("大家好，我来了");
        };
        ws.onmessage = function(e) {
            console.log("client：接收到服务端的消息 " + e.data);
            addRoomMessage(e.data);
        };
        ws.onclose = function(params) {
            console.log("client：关闭连接");
        };
    }
    function disConnect() {
        if(server != null) {
            server.close();
            server = null;
        }
    }

    function doSendMessage() {
        let msg = $("#mymessge").val();
        if(msg == "") {
            return;
        }
        if(server == null) {
            console.log("没有连接");
            return;
        }

        $("#mymessge").val("");
        server.send(msg);
    }

    function addRoomMessage(msg) {
        $("#chatroom").append(msg + "<br />");
    }

</script>
</body>
</html>